import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Register from '../components/Register.vue'
import Home from '../components/Home.vue'
import MovieInfo from '../components/movieinfo/MovieInfo.vue'
import Category from '../components/movieinfo/Category.vue'
import User from '../components/movieinfo/User.vue'
import Details from '../components/movieinfo/Details'
import Car from '../components/movieinfo/Car.vue'
import Order from '../components/movieinfo/Order.vue'
import Evaluation from '../components/movieinfo/Evaluation.vue'
import Search from '../components/movieinfo/Search.vue'
import Published from '../components/movieinfo/Published.vue'
import Storeup from '../components/movieinfo/Storeup.vue'
import Setting from '../components/movieinfo/Setting.vue'
import Userinfo from '../components/Userinfo.vue'

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect: 'login'
        },
        {
            path: '/login',
            component: Login
        },
        {
            path: '/register',
            component: Register
        },
        {
            path: '/home',
            component: Home,
            redirect: 'user',
            children: [
                {
                    path: '/movieinfo',
                    component: MovieInfo
                },
                {
                    path: '/category',
                    component: Category
                },
                {
                    path: '/user',
                    component: User
                }
            ]
        },
        {
            path: '/details/:id',
            component: Details
        },
        {
            path: '/car',
            component: Car
        },
        {
            path: '/order',
            component: Order
        },
        {
            path: '/evaluation',
            component: Evaluation
        },
        {
            path: '/search',
            component: Search
        },
        {
            path: '/published',
            component: Published
        },
        {
            path: '/storeup',
            component: Storeup
        },
        {
            path: '/setting',
            component: Setting
        },
        {
            path:'/userinfo',
            component: Userinfo
        }
    ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    if (to.path == '/login' || to.path == '/register') return next()
    const token = window.sessionStorage.getItem('token')
    if (!token) return next('/login')
    next()
})

export default router